require 'ajax_helper'

class QuestionsController < ApplicationController

  include AjaxHelper
  before_filter :login_required, :except => [:index, :list, :show]

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @question_pages, @questions = paginate :questions, :per_page => 10
  end
  
  # list login
  def list_login
    # this can work because we specified before_filter at the beginning of controller
    # so it will automatically call login, and after that, we get back to the list
    redirect_to :action => :list
  end

  def show
    @question = Question.find(params[:id])
    @answer_pages, @answers = paginate :answers, :order => 'id DESC', :conditions=> "question_id=#{params[:id]}"
  end
  
  def show_login
    # this can work because we specified before_filter at the beginning of controller
    # so it will automatically call login, and after that, we get back to the list
    redirect_to :action => :show
  end

  def new
    @question = Question.new
  end

  def create

    # create new question
    @question = Question.new(params[:question])
    @question.tag_with(params[:tag_list])
    
    @user = User.find(session[:user][:id])
    @user.questions << @question
    
    if @question.save
      flash[:notice] = 'Question was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  # user add comments
  def add_answer
  
    # sanity check
    if (!params[:answer] || !params[:answer][:answer] || 
        !params[:question] || !params[:question][:id] || !session[:user])
      redirect_to :action=>'show', :id=>@deal.id
      return
    end
    
    # find user & deal first    
    @user = User.find(session[:user][:id])
    @question = Question.find(params[:question][:id])

    if (@user && @question)
    
      # add new anser
      @answer = Answer.new(params[:answer])
 
      # add rating to user rating
      if (@answer)
        @user.answers << @answer
        @question.answers << @answer
        #@usercomment.user_id = @user.id
        #@usercomment.deal_id = @deal.id
        #@usercomment.comments = params[:user_dealcomment][:comments]
        @user.save
        @question.save
      end
    end
    
    #rescue
      # do nothing for ajax    
    #ensure
      redirect_to :action=>'show', :id=>@question.id
      #render :action=>'show', :id=>@deal.id
  end
  
  ##################################################################
  # private functions
  ##################################################################
  
  def destroy
    Question.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def edit
    @question = Question.find(params[:id])
  end

  def update
    @question = Question.find(params[:id])
    @question.tag_with(params[:tag_list])
    if @question.update_attributes(params[:question])
      flash[:notice] = 'Question was successfully updated.'
      redirect_to :action => 'show', :id => @question
    else
      render :action => 'edit'
    end
  end
  
end
